package com.socialuni.center.web.serive;

import com.socialuni.center.web.model.VO.TalkAddVO;
import com.socialuni.center.web.utils.DevAccountUtils;
import com.socialuni.sdk.constant.ContentType;
import com.socialuni.sdk.constant.status.ContentStatus;
import com.socialuni.sdk.model.DO.DistrictDO;
import com.socialuni.sdk.model.DO.talk.TalkDO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.RectangleVO;
import com.socialuni.sdk.platform.AliAPI;
import com.socialuni.sdk.store.TalkQueryStore;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author qinkaiyuan
 * @date 2020-05-16 20:11
 */
@Service
public class TalkService {
    @Resource
    TalkQueryStore talkQueryStore;

    public TalkDO addTalk(TalkAddVO talkAddVO, DistrictDO district, UserDO user, String visibleGender) {
        TalkDO talkDO = new TalkDO();
        Date curdate = new Date();
        talkDO.setCreateTime(curdate);
        talkDO.setUpdateTime(curdate);
        talkDO.setContent(talkAddVO.getContent());
        talkDO.setVisibleType(talkAddVO.getVisibleType());
        talkDO.setHugNum(0);
        talkDO.setCommentNum(0);
        talkDO.setReportNum(0);
        talkDO.setGlobalTop(0);
        talkDO.setStatus(ContentStatus.enable);
        //已在外层经过校验

        talkDO.setUserId(user.getId());

        talkDO.setReportContentType(ContentType.talk);

        //如果经纬度为空
        if (talkAddVO.getLon() == null || talkAddVO.getLat() == null) {
            //如果经纬度为空
            RectangleVO rectangleVO = AliAPI.getRectangle();
            if (rectangleVO != null) {
                talkAddVO.setLon(rectangleVO.getLon());
                talkAddVO.setLat(rectangleVO.getLat());
            }
        }
//        PositionDO positionDO = new PositionDO(user.getId(), districtDO, this.lon, this.lat);
//        positionDO = PositionUtils.save(positionDO);
//        talkDO.setPositionId(positionDO.getId());

        //使用talk本身存储,position 和 district
        talkDO.setAdCode(district.getAdCode());
        talkDO.setAdName(district.getAdName());
        talkDO.setProvinceName(district.getProvinceName());
        talkDO.setCityName(district.getCityName());
        talkDO.setDistrictName(district.getDistrictName());
        talkDO.setLon(talkAddVO.getLon());
        talkDO.setLat(talkAddVO.getLat());

        talkDO.setVisibleGender(visibleGender);
        talkDO.setDevId(DevAccountUtils.getDevId());
        return talkDO;
    }

    public List<TalkDO> queryTalksTop10ByUser(List<Integer> talkIds, Integer userId) {
        int page = talkIds.size() / 10;
        List<Integer> ids = talkQueryStore.queryUserTalkIds(userId, PageRequest.of(page, 10));
        return talkQueryStore.queryTalksByIds(ids);
    }

    //查看自己的动态，能查看到预审查状态的
    public List<TalkDO> queryTalksTop10ByMine(List<Integer> talkIds, Integer userId) {
        int page = talkIds.size() / 10;
        List<Integer> ids = talkQueryStore.queryMineTalkIds(userId, PageRequest.of(page, 10));
        return talkQueryStore.queryTalksByIds(ids);
    }

    /*public List<TalkDO> queryTalksTop10ByUserFollow(List<Integer> talkIds, Integer userId) {
        List<Integer> beUserIds = followStore.queryUserFollowUserIds(userId);
        int page = talkIds.size() / 10;
        List<Integer> ids = talkQueryStore.queryUserFollowsTalkIds(userId, beUserIds, PageRequest.of(page, 10));
        return talkQueryStore.queryTalksByIds(ids);
    }*/
}
